package com.three.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.three.entity.*;
import com.three.mapper.BranchGoodsMapper;
import com.three.mapper.GoodsMapper;
import com.three.mapper.InlibraryMapper;
import com.three.entity.Branch;
import com.three.entity.Goods;
import com.three.entity.Staff;
import com.three.mapper.*;
import com.three.service.GoodsService;
import com.three.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private HttpSession session;
    @Autowired
    private BranchMapper branchMapper;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private BranchGoodsMapper branchGoodsMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public R list(Integer pageNum, Integer pageSize, KeyboardVO keyboardVO) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        Staff staff = (Staff) session.getAttribute("staff");//获取当前登录用户
        if (!staff.getBranchid().equals(12138)) {
            keyboardVO.setBranchId(staff.getBranchid());//获取当前登录用户的门店id
            List<Map<String, Object>> goods = goodsMapper.selectgoods(keyboardVO);//查询
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goods);
            return R.ok(pageInfo);
        }
        //查询当前登录
        List<Map<String, Object>> goods = goodsMapper.selectgoods(keyboardVO);
        //封装分页数据
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goods);
        return R.ok(pageInfo);
    }

    //新增
    @Override
    public R add(Goods goods) {
        //新增
        int i = goodsMapper.insert(goods);
        if (i > 0) {
            return R.ok();
        }
        return R.error("新增失败");
    }
//编辑
    @Override
    public R edit(Goods goods) {
        int i = goodsMapper.updateByPrimaryKey(goods);
        if (i > 0) {
            return R.ok();
        }
        return R.error("修改失败");
    }
//删除
    @Override
    public R delete(Integer goodsid) {
        int i = goodsMapper.deleteByPrimaryKey(goodsid);
        if (i > 0) {
            return R.ok();
        }
        return R.error("删除失败");
    }

    //新增入库按钮的展示所有
    @Override
    public R selectGoodsAll(Integer pageNum, Integer pageSize, Integer branchid) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        Staff staff = (Staff) session.getAttribute("staff");//获取当前登录用户
        branchid = staff.getBranchid();//获取当前登录用户的门店id
        List<Map<String, Object>> list = goodsMapper.selectGoodsAll(branchid);
        //封装分页数据
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    //查询所有
    @Override
    public R getAllGoods(Integer currentPage, Integer pageSize, KeyboardVO keyboardVO) {
        Staff staff = (Staff) session.getAttribute("staff");
        if (!staff.getBranchid().equals(12138)) {
            keyboardVO.setBranchId(staff.getBranchid());
        }
        //开启分页
        PageHelper.startPage(currentPage, pageSize);
        List<Map<String, Object>> list = goodsMapper.selectgoodsyuan(keyboardVO);
        //封装分页数据
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    @Override
    public R getAllOutGoods(Integer currentPage, Integer pageSize, AddOutLibrartVo addOutLibrartVo) {
        //开启分页
        PageHelper.startPage(currentPage, pageSize);
        //判断是否有权限看所有的店
        Staff staff = (Staff) session.getAttribute("staff");
        if (!staff.getBranchid().equals(12138)) {
            addOutLibrartVo.setBranchid(staff.getBranchid());
        }
        //查询
        List<Map<String, Object>> goods = goodsMapper.getAllOutGoods(addOutLibrartVo);
        //封装分页数据
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(goods);
        return R.ok(pageInfo);
    }


    //点击新增商品展示所有商品
    @Override
    public R listgoods(Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> list = goodsMapper.selectList();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }


    //添加
    @Override
    public R addGoodsAll(List<AddintLibraryVO> addintLibraryVOList) {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();//获取当前登录用户的门店id
        KeyboardVO keyboardVO = new KeyboardVO();//查询所有商品
        keyboardVO.setBranchId(branchid);//获取当前登录用户的门店id
        List<Map<String, Object>> goods = goodsMapper.selectgoodsyuan(keyboardVO);
     // 循环遍历，判断是否有重复商品
        for (AddintLibraryVO goods1 : addintLibraryVOList) {//循环遍历新增商品
            for (Map<String, Object> map : goods) {//循环遍历所有商品
                //根据 商品id判断是否有
                if (goods1.getGoodsid().equals(map.get("goodsid"))) {
                    return R.error("商品已存在");
                }
            }
            //没有则新增
            BranchGoods branchGoods = new BranchGoods();//封装数据
            branchGoods.setGoodsid(goods1.getGoodsid());//商品id
            branchGoods.setBranchid(branchid);//门店id
            branchGoods.setCounts(0);//商品数量
            branchGoodsMapper.insertSelective(branchGoods);//新增
        }

        return R.ok();
    }


    //获取当前登录用户的id
    @Override
    public R getUserStaffId() {
        Staff staff = (Staff) session.getAttribute("staff");
        return R.ok(staff.getStaffid());
    }



    //获取商品售价统计信息,查询商品的名称和进价售价 首页统计图 1
    @Override
    public R getGoodsAllInfo() {
        List<Goods> allNameJingShou = goodsMapper.getAllNameJingShou();
        List<String> goodsName=new ArrayList<>();
        List<Double> cprice=new ArrayList<>();
        List<Double> sprice=new ArrayList<>();
        for (Goods goods : allNameJingShou) {
            goodsName.add(goods.getGoodsname());
            cprice.add(goods.getCprice());
            sprice.add(goods.getSprice());
        }
        Map<String,Object> map=new HashMap<>();
        map.put("goodsName",goodsName);
        map.put("cprice",cprice);
        map.put("sprice",sprice);
        return R.ok(map);
    }

    //获取店铺数目，员工数目，商品总数
    @Override
    public R getBranchStaffGoodsInfo() {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer staffid = staff.getStaffid();
        Integer branchid = staff.getBranchid();
        //查询门店数目
        Integer bcounts=branchMapper.getBranchCounts();
        if (bcounts==null){
            return R.error("获取失败");
        }else {
            //查询本门店员工数目
            Integer scounts=staffMapper.getStaffCounts(branchid);
            if (scounts==null){
                return R.error("获取失败");
            }else {
                //根据branchid查询有几件商品
                Integer bgcounts =branchGoodsMapper.getGoodsCounts(branchid);
                Map<String,Object> map=new HashMap<>();
                map.put("bcounts",bcounts);
                map.put("scounts",scounts);
                map.put("bgcounts",bgcounts);
                return R.ok(map);
            }
        }
    }

    //获取部门员工数量
    @Override
    public R getDeptStaffCounts() {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        List<DeptNameAndCountsVo> aaa = departmentMapper.getDeptStaffCounts(branchid);
        if (aaa==null) {
            return R.error("获取失败");
        }else {
            return R.ok(aaa);
        }
    }

    //计算本门店商品库存统计
    @Override
    public R getGoodsAndNameCounts() {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        List<GoodsNameAndCountVo> ac = branchGoodsMapper.getGoodsAndNameCounts(branchid);
        if (ac==null){
            return R.error("获取失败");
        }else {
        List<String> goodsName=new ArrayList<>();
        List<Integer> goodsCounts=new ArrayList<>();
        for (GoodsNameAndCountVo goodsNameAndCountVo : ac) {
            goodsName.add(goodsNameAndCountVo.getName());
            goodsCounts.add(goodsNameAndCountVo.getValue());
        }
        Map<String,Object> map=new HashMap<>();
        map.put("goodsName",goodsName);
        map.put("goodsCounts",goodsCounts);
        return R.ok(map);
        }
    }

    //获取当前登录用户的信息
    @Override
    public R getStaffAll() {
        Staff s = (Staff) session.getAttribute("staff");
        Staff staff=staffMapper.selectAllStaff(s);
        if (staff!=null){
            return R.ok(staff);
        }else {
            return R.error("获取失败");
        }
    }




}
